package main

import (
	"fmt"
	"sync"
)

func main() {
	var group sync.WaitGroup
	var mutex sync.RWMutex

	mp := make(map[string]int)
	group.Add(10)

	for i := 0; i < 10; i++ {
		go func(id int) {
			// 写操作 - 需要互斥锁
			for j := 0; j < 100; j++ {
				mutex.Lock()
				mp["helloworld"] = j
				mutex.Unlock()
			}

			// 读操作 - 使用读锁
			for j := 0; j < 10; j++ {
				mutex.RLock()
				value := mp["helloworld"]
				mutex.RUnlock()
				fmt.Printf("Goroutine %d: %d\n", id, value)
			}

			group.Done()
		}(i) // 传递循环变量
	}
	group.Wait()
}
